﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;
namespace DAO
{
    public class LoaiKHDA
    {
        public static LoaiKH GetByID(int maID)
        {
            LoaiKH loai = null;
            SqlConnection cn = DataProvider.Connect();
            //string str = "select @ten,@cm,@passpost,@ngaysinh,@diachi,@masothue,@giayphepkd,@nguoidaidien,@tenloai from khachhang,loaikh where maloai=loaikh,makh= " + @ma.ToString();
            SqlCommand cmd = new SqlCommand("Proc_LoaiKH_GetById", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@maLoaiKH", maID));
            SqlDataReader dataReader = cmd.ExecuteReader();
            if (dataReader.Read())
            {
                loai = new LoaiKH();
                loai.MaLoai = maID;
                loai.TenLoai = Convert.ToString(dataReader["TenLoai"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
            }
            cn.Close();
            return loai;
        }

        public static List<LoaiKH> GetALl(int delFlg)
        {
            List<LoaiKH> dsLoaiKH= new List<LoaiKH>();
            LoaiKH loai = null;
            SqlConnection cn = DataProvider.Connect();
            SqlCommand cmd = new SqlCommand("Proc_LoaiKH_GetALL", cn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@delFlg",delFlg));

            SqlDataReader dataReader = cmd.ExecuteReader();
            while (dataReader.Read())
            {
                loai = new LoaiKH();
                loai.MaLoai = Convert.ToInt32(dataReader["MaLoai"]);
                loai.TenLoai = Convert.ToString(dataReader["TenLoai"]);
                loai.LastUpdate = Convert.ToDateTime(dataReader["LastUpdate"]);
                loai.DelFlg = Convert.ToInt32(dataReader["DelFlg"]);
                dsLoaiKH.Add(loai);
            }
            cn.Close();
            return dsLoaiKH;
        }
    }
}
